// ==UserScript==
// @name         企企开发增强
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  增加一些便于开发的功能
// @author       wangshushuo
// @match        http://localhost:3000/*
// @match        https://*.e7link.com/*
// @match        https://*.77hub.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=160.10
// @grant        GM_addStyle
// @grant        GM_addElement
// @grant        GM_setClipboard
// @grant        GM_notification
// @grant        unsafeWindow
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function () {
  "use strict";
  GM_addStyle(`
  .dev-info-bar-parent {
    height: 20px;
    position: relative;
    width: 100vw;
  }
  #dev-inject--dev-info-bar{
    position: absolute;
    z-index: 999999;
    width: 100vw;
  }
  .bp3-dialog-container {
    backdrop-filter: saturate(180%) blur(2px);
  }
  .bp3-overlay-scroll-container .bp3-overlay-backdrop {
    background-color: rgba(0, 0, 0, 0.2) !important;
  }

  [class^="LayoutFullScreen-styles_leftContainer"] {
    display: none;
  }
  [class^="LayoutFullScreen-styles_rightContainer"] {
    margin:auto;
  }
  `)

    const menu_command_id_1 = GM_registerMenuCommand(
      "Show Alert",
      function (event) {
        alert("Menu item selected");
      },
      {
        accessKey: "a",
        autoClose: true,
      }
    );

    const menu_command_id_2 = GM_registerMenuCommand(
      "Log",
      function (event) {
        console.log("Menu item selected");
      },
      "l"
    );
    const menu_command_id_3 = GM_registerMenuCommand(
      "password to text",
      function (event) {
        console.log("Menu item selected", event);
        event.target.type = "text";
      }
    );

  var d = {...window.console}
  var oldLog = d.log
  d.log = (...args) => {
      if(!args) return
      var m = args[0]
      if(m && typeof m == 'string' && m.indexOf('77ws')==0) return
      oldLog(...args)
  }
  unsafeWindow.dd = d
  unsafeWindow.ccc = () => {
    unsafeWindow.console = d
  }
  setTimeout(() => {
    unsafeWindow.console = d
  }, 5000)

  findElement('#dev-inject--dev-info-bar').then(bar => {
    bar.parentElement.classList.add('dev-info-bar-parent')
    createButton(bar)
  })
  function createButton(bar) {
    const a = GM_addElement(
      bar,
      'div',
      {
        style: 'display: inline-flex;'
      }
    )
    const b = GM_addElement(
      a,
      'button',
      {
        textContent: '🚀',
        style: 'display: inline-flex;'
      }
    )
    b.onclick = function (e) {
      enhanceForm()
    }
  }

  // 针对form的增强
  function enhanceForm() {
    document.querySelectorAll('.atx-section-header-title').forEach(titleEl => {
      titleEl.appendChild(setFormTemplateButton())
      titleEl.appendChild(presenterNameButton())
    })
  }

  // 打开预制数据的表单对应页面
  function setFormTemplateButton() {
    const bt = document.createElement('button')
    bt.textContent = '预制数据 🚗'
    bt.onclick = function (e) {
      const entity = bizFormPresenter.api.formController.entityCRUD.entity.name
      location.hash = `#/dev-tools/form/schemas/edit/${entity}`
    }
    return bt
  }

  // form presenter 对应的 class name
  function presenterNameButton() {
    const bt = document.createElement('button')
    bt.textContent = 'presenter class name 🚲'
    bt.onclick = function (e) {
      const presenterClassName = bizFormPresenter.api.tabApi.getInstance().component.props.presenter.constructor.name
      console.log('presenterClassName: ', presenterClassName);
      if (window.confirm(presenterClassName + " (点ok复制到剪贴板)")) {
        GM_setClipboard(presenterClassName, "text");
      }
      // GM_notification({
      //   text: "表单对应的 presenter 的 class name",
      //   title: presenterClassName + '\n 点击复制到剪贴板',
      //   onclick: () => {
      //     GM_setClipboard(presenterClassName, "text");
      //   }
      // });
    }
    return bt
  }

  // common code -- start ...
  function debounce(func, timeout = 1000) {
    let timer;
    return (...args) => {
      clearTimeout(timer);
      timer = setTimeout(() => {
        func.apply(this, args);
      }, timeout);
    };
  }

  function findElement(selector) {
    return new Promise((resolve, reject) => {
      const interval = setInterval(() => {
        const el = document.querySelector(selector);
        if (el) {
          clearInterval(interval);
          resolve(el);
        }
      }, 1000);
    });
  }

  // common code -- end ...
})();
